package com.ielts.activity;

import com.ielts.IeltsConstant;
import com.ielts.services.IeltsServices;

import vn.com.baseapplication.BaseActivity;
import vn.com.baseapplication.constant.BaseConstant;
import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.webkit.SslErrorHandler;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

@SuppressLint({ "SetJavaScriptEnabled", "NewApi" })
public class ChargeMoneyActivity extends BaseActivity{
	WebView wv;
	TextView year;
	ImageView arrow;
	
	IeltsServices mServices;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_charge);
		year = (TextView) findViewById(R.id.header_tv_year);
		arrow = (ImageView) findViewById(R.id.header_iv_arrow);
		wv = (WebView)findViewById(R.id.wv);
		
		year.setText("Deposit money");
		arrow.setVisibility(View.INVISIBLE);
		
		wv.setBackgroundColor(0x00000000);
		if (Build.VERSION.SDK_INT >= 11) wv.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);
		wv.setWebViewClient(new WebViewClient()
		{
		    @Override
		    public void onPageFinished(WebView view, String url)
		    {
		        view.setBackgroundColor(0x00000000);
		        if (Build.VERSION.SDK_INT >= 11) view.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);
		    }
		});
		
		WebSettings settings = wv.getSettings();
		settings.setJavaScriptEnabled(true);
		settings.setDomStorageEnabled(true);

		
		
		wv.setWebViewClient(new WebViewClient() {
			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				super.onPageStarted(view, url, favicon);
			}

			@Override
			public void onPageFinished(WebView view, String url) {
				super.onPageFinished(view, url);
			}

			@Override
			public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
				super.onReceivedSslError(view, handler, error);

				// this will ignore the Ssl error and will go forward to your
				// site
				handler.proceed();
			}
		});
		
		String link = getIntent().getStringExtra(BaseConstant.MSG_DATA);
		
		wv.loadUrl(link);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		if (!isServiceConnected()) {
			bindBaseService(IeltsServices.class);
		} else {

		}
	}
	
	@Override
	public void onServiceConnected() {
		super.onServiceConnected();
		mServices = (IeltsServices) getBaseServices();
		Log.e(TAG, mServices.getHelloString());
		if (IeltsApplication.getInstance().getValue(IeltsConstant.KEY_USER_ID, IeltsConstant.KEY_USER_ID_DEF).equals(IeltsConstant.KEY_USER_ID_DEF)) {
			mServices.registerUserPost();
		} else
			mServices.getUserBalance();
	}
	
	@SuppressWarnings("static-access")
	@Override
	protected void onDestroy() {
		super.onDestroy();
		if(mServices!=null && IeltsApplication.getInstance().haveInternet(this)){
			mServices.getUserBalance();
		}
	}
}
